<template>
  <div class="dashboard-container">
    <div class="app-container">
      <el-card shadow="never">
        <div class="Btnbox">
          <span>{{$t('table.notice')}}</span>
          <el-button type="success" icon="el-icon-edit" size="small" @click="$router.push('new')">
            {{$t('table.questionNew')}}
          </el-button>
        </div>
        <!-- 输入框结构 -->
        <!-- 第一行 -->
        <el-form ref="selectform" :model="articleQuery" label-width="80px" class="filter-container">
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item :label="$t('route.subject')">
                <el-select v-model="articleQuery.subjectID" @change="subChange" class="selectwidth">
                  <el-option :label="item.label" :value="item.value" v-for="(item, index) in subjectList" :key="index">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item :label="$t('route.secondDir')">
                <el-select v-model="articleQuery.catalogID" class="selectwidth">
                  <el-option :label="item.label" :value="item.value" v-for="(item, index) in directory" :key="index">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item :label="$t('route.tag')">
                <el-select v-model="articleQuery.tags" class="selectwidth">
                  <el-option :label="item.label" :value="item.value" v-for="(item, index) in tag" :key="index">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item :label="$t('route.keyWord')">
                <el-input v-model="articleQuery.keyword" class="selectwidth"></el-input>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 第二行 -->
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item :label="$t('route.type')">
                <el-select v-model="articleQuery.questionType" class="selectwidth">
                  <el-option :label="item.label" :value="item.value" v-for="(item, index) in questionType" :key="index">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item :label="$t('route.difficulty')">
                <el-select v-model="articleQuery.difficulty" class="selectwidth">
                  <el-option :label="item.label" :value="item.value" v-for="(item, index) in difficulty" :key="index">
                  </el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item :label="$t('route.direction')">
                <el-select v-model="articleQuery.direction" class="selectwidth">
                  <el-option :label="item" :value="item" v-for="(item, index) in direction" :key="index"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item :label="$t('route.creator')">
                <el-select v-model="articleQuery.creatorID" class="selectwidth">
                  <el-option :label="item.username" :value="item.id" v-for="item in creator" :key="item.id"></el-option>
                </el-select>
              </el-form-item>
            </el-col>
          </el-row>
          <!-- 第三行 -->
          <el-row :gutter="20">
            <el-col :span="6">
              <el-form-item :label="$t('route.remarks')">
                <el-input v-model="articleQuery.remarks" class="selectwidth"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item :label="$t('route.company')">
                <el-input v-model="articleQuery.shortName" class="selectwidth"></el-input>
              </el-form-item>
            </el-col>
            <el-col :span="6">
              <el-form-item :label="$t('route.city')" style="display:inline-block">
                <el-row class="selectwidth">
                  <el-col :span="12" style="padding-right:5px">
                    <el-select v-model="articleQuery.province" @change="selectCity">
                      <el-option :label="item" :value="item" v-for="(item, index) in provinces" :key="index">
                      </el-option>
                    </el-select>
                  </el-col>
                  <el-col :span="12" style='padding-left: 5px;'>
                    <el-select v-model="articleQuery.city">
                      <el-option :label="item" :value="item" v-for="(item, index) in citys" :key="index"></el-option>
                    </el-select>
                  </el-col>
                </el-row>
              </el-form-item>
            </el-col>
            <el-col :span="6" class="addBtn" style='padding-right: 0;'>
              <el-button size="small" @click="clearForm">{{$t('route.clear')}}</el-button>
              <el-button type="primary" size="small" @click="getBasicQuestion">{{$t('navbar.search')}}</el-button>
            </el-col>
          </el-row>
        </el-form>
        <!-- alert提示消息 -->
        <el-alert :title="'数据一共' + toatalCount + '条'" type="info" show-icon :closable="false"></el-alert>
        <!-- 数据列表 -->
        <el-table :data="QuestionBankData" style="width: 100%;margin-top:15px">
          <el-table-column align="center" prop="number" :label="$t('route.number')" width="120"></el-table-column>
          <el-table-column align="center" prop="subject" :label="$t('route.subject')" width="90"></el-table-column>
          <el-table-column align="center" prop="catalog" :label="$t('route.directory')" width="90"></el-table-column>
          <el-table-column align="center" prop="questionType" :label="$t('route.type')" width="80">
            <template slot-scope="scope">
              <span>{{ scope.row.questionType | ConversionQuestionType }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="question" :label="$t('table.stem')" width="220" show-overflow-tooltip>
            <template slot-scope="scope">
              <div v-html="scope.row.question"></div>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="addDate" :label="$t('table.creatDate')" width="180">
            <template slot-scope="scope">
              <span>{{ parseTime(scope.row.addDate) }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="difficulty" :label="$t('route.difficulty')" width="120">
            <template slot-scope="scope">
              <span>{{ scope.row.questionType | ConversionDifficulty }}</span>
            </template>
          </el-table-column>
          <el-table-column align="center" prop="creator" :label="$t('route.creator')" width="120"></el-table-column>
          <el-table-column align="center" prop="address" :label="$t('table.actions')" width="200">
            <template slot-scope="scope">
              <el-button type="primary" plain size="mini" round icon="el-icon-view" title="预览"
                @click="viewHandel(scope.row)" circle></el-button>
              <el-button type="success" plain size="mini" round icon="el-icon-edit" title="修改"
                @click="toEditePage(scope.row.id)" circle></el-button>
              <el-button type="danger" plain size="mini" round icon="el-icon-delete" title="删除"
                @click="deleteQuestions(scope.row.id)" circle></el-button>
              <el-button type="warning" plain size="mini" round icon="el-icon-check" title="加入精选"
                @click="AddChoiceness(scope.row.id)" circle></el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页结构 -->
        <el-pagination class="pagination" @size-change="handleSizeChange" @current-change="handleCurrentChange"
          :current-page.sync="articleQuery.page" background :page-sizes="[5, 10, 15, 20]"
          :page-size="articleQuery.pagesize" :total="toatalCount" layout="prev, pager, next,sizes, jumper">
        </el-pagination>
      </el-card>
    </div>
    <!-- 试题预览弹层 -->
    <el-dialog title="题目预览" :visible.sync="PreviewDialog" width="60%" :before-close="handleClose">
      <preview-article :currentData="currentData" v-if="PreviewDialog"></preview-article>
      <span slot="footer" class="dialog-footer">
        <el-button type="primary" @click="PreviewDialog = false">关闭</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import { simple as subjectsSimple } from '@/api/hmmm/subjects'
import { simple as directorysSimple } from '@/api/hmmm/directorys'
import { simple as tagSimple } from '@/api/hmmm/tags'
import { questionType, difficulty, direction } from '@/api/hmmm/constants'
import { simple as usersSimple } from '@/api/base/users'
import { provinces, citys } from '@/api/hmmm/citys'
import {
  list as questionsList,
  remove as questionsRemove,
  choiceAdd as AddToChoiceness
} from '@/api/hmmm/questions'
import { parseTime } from '@/utils/index'
import PreviewArticle from '../components/preview-article'
export default {
  components: {
    PreviewArticle: PreviewArticle
  },
  data () {
    return {
      // 文章搜索数据
      articleQuery: {
        // 学科id
        subjectID: null,
        // 二级目录
        catalogID: null,
        // $t('route.tag')内容
        tags: null,
        // 关键字
        keyword: '',
        // 试题类型
        questionType: null,
        // 难度
        difficulty: null,
        // 方向
        direction: '',
        // 录入人
        creatorID: null,
        // 题目备注
        remarks: '',
        // 企业简称
        shortName: '',
        // 企业所在省份
        province: '',
        // 企业所在城市
        city: '',
        // 数据当前页码
        page: 1,
        // 每页显示数据数
        pagesize: 5
      },
      // 学科简单列表
      subjectList: [],
      // 二级目录列表
      directory: [],
      // $t('route.tag')数据
      tag: [],
      // 录入人数据
      creator: [],
      // 省份数据
      provinces: [],
      // 城市数据
      citys: [],
      // 基础题库总数
      toatalCount: 0,
      // 题库数据
      QuestionBankData: [],
      // 控制题目预览对话框显示隐藏
      PreviewDialog: false,
      // 当前行的数据
      currentData: {}
    }
  },
  created () {
    this.getSubjectSimple()
    this.provinces = provinces()
    this.getBasicQuestion()
    this.getUsers()
  },
  methods: {
    // 获取学科简单列表
    async getSubjectSimple () {
      const { data } = await subjectsSimple()
      // console.log(data)
      this.subjectList = data
    },
    // 学科内容发生改变执行
    subChange () {
      this.getsubjects()
      this.getTag()
    },
    // 获取二级目录
    async getsubjects () {
      const subjectID = this.articleQuery.subjectID
      const { data } = await directorysSimple({ subjectID: subjectID })
      // console.log(data)
      // 每次发起请求，将数据清空
      this.$refs.selectform.clearValidate()
      this.articleQuery.catalogID = null
      this.directory = data
    },
    // 获取$t('route.tag')列表
    async getTag () {
      const { data } = await tagSimple({
        subjectID: this.articleQuery.subjectID
      })
      // console.log(data)
      this.tag = data
    },
    // 省份选中后加载城市
    selectCity () {
      this.citys = citys(this.articleQuery.province)
    },
    // 获取用户数据
    async getUsers () {
      const { data } = await usersSimple()
      this.creator = data
    },
    // 每页条数的改变
    handleSizeChange (val) {
      // console.log('当前每页条数', val)
      this.articleQuery.pagesize = val
      this.getBasicQuestion()
    },
    // 当前页面
    handleCurrentChange (val) {
      // console.log('当前页码', val)
      this.articleQuery.page = val
      this.getBasicQuestion()
    },
    // 获取基础题库列表
    async getBasicQuestion () {
      const data = await questionsList(this.articleQuery)
      // console.log(data)
      if (data.status === 200) {
        // console.log(data.data)
        this.toatalCount = data.data.counts
        this.QuestionBankData = data.data.items
      } else {
        this.$message.error('数据获取失败，请刷新网页')
      }
    },
    // 清除form查询条件
    clearForm () {
      // console.log(this.$refs.selectform)
      this.$refs.selectform.$el.reset()
      this.articleQuery.subjectID = null
      this.articleQuery.catalogID = null
      this.directory = []
      // console.log(this.articleQuery)
      this.getBasicQuestion()
    },
    // 预览对话框关闭的回调
    handleClose () {
      // console.log(1)
      this.PreviewDialog = false
    },
    // 点击预览执行函数
    viewHandel (val) {
      this.PreviewDialog = true
      // console.log(val)
      this.currentData = { id: val.id }
    },
    // 基础题库删除
    deleteQuestions (id) {
      // console.log(id)
      this.$confirm('此操作将永久删除该试题, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 发送请求,删除试题
          const { data } = await questionsRemove({ id })
          this.getBasicQuestion()
          // console.log(data)
          if (data.success) {
            this.$message.success('删除成功')
          }
        })
        .catch(() => {
          this.$message.info('已取消删除')
        })
    },
    // 加入精选
    AddChoiceness (id) {
      // console.log(id)
      this.$confirm('此操作会将该试题添加到精选题库, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async () => {
          // 发送请求,删除试题
          const { data } = await AddToChoiceness({
            id: id,
            choiceState: 1
          })
          console.log(data)
          this.getBasicQuestion()
          if (data.success) {
            this.$message.success('添加成功')
          }
        })
        .catch(() => {
          this.$message.info('已取消添加')
        })
    },
    // 点击编辑按钮,跳转到编辑页面
    toEditePage (id) {
      this.$router.push({
        path: 'new',
        query: {
          id
        }
      })
    }
  },
  computed: {
    questionType () {
      return questionType
    },
    difficulty () {
      return difficulty
    },
    direction () {
      return direction
    },
    parseTime () {
      return parseTime
    }
  },
  filters: {
    ConversionQuestionType (typeVal) {
      // console.log(typeVal, questionType)
      return questionType[typeVal - 1].label
    },
    ConversionDifficulty (difVal) {
      // console.log(difficulty, difVal)
      return difficulty[difVal - 1].label
    }
  }
}
</script>

<style lang="scss" scoped>
.Btnbox {
  display: flex;
  justify-content: space-between;
  margin-bottom: 15px;
  > span {
    font-size: 12px;
    color: pink;
  }
}
.addBtn {
  text-align: right;
  margin-bottom: 10px;
}
.el-row {
  margin-bottom: 0px;
  &:last-child {
    margin-bottom: 0;
  }
}
.el-col {
  border-radius: 4px;
}
.citySelectForm {
  float: left;
}
.filter-container {
  margin-top: 20px;
  ::v-deep .el-col-6 {
    .el-form-item {
      width: 100%;
      .el-form-item__content {
        width: 100%;
        .selectwidth {
          width: 76%;
          height: 32px;
        }
      }
    }
  }
}
.pagination {
  float: right;
  margin: 20px 0;
}
</style>
